package pers.cnzdy.tutorial.Chapter9_Perception_Media.Section6_MaterialDesign;

//import android.support.design.widget.NavigationView;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import pers.cnzdy.tutorial.R;

public class SlideMenuActivity extends AppCompatActivity {
    private static final String TAG = SlideMenuActivity.class.getSimpleName();
    private DrawerLayout slideMenuDrawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_design);

        // 注意将style.xml中的
        // <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">-->
        // 改为：<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_bird);

            Log.v(TAG, "加载ActionBar");
        }

        // 滑动菜单
        slideMenuDrawerLayout = findViewById(
                R.id.slide_menu_drawer_layout);

        NavigationView navView = findViewById(R.id.nav_view);
        navView.setCheckedItem(R.id.slide_photo);
        navView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                slideMenuDrawerLayout.closeDrawers();
                return true;
            }
        });
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                slideMenuDrawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.query:
                Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
                break;
            case R.id.add:
                Toast.makeText(this, "增加", Toast.LENGTH_SHORT).show();
            default:
                break;
        }
        return true;
    }
}